#include <string.h>

#include "els.h"

#include "els_mem.h"
#include "els_object.h"
#include "els_vmhost.h"
#include "els_string.h"

union L_UTString
{
    TString ts;
    union L_Umaxalign dummy;
};

void els_string_init(els_VmObj *L)
{
    L->strt.hash = els_Mem_newvector(L, 1, TString *);
    L->udt.hash = els_Mem_newvector(L, 1, TString *);
    L->nblocks += 2 * sizeof(TString *);
    L->strt.size = L->udt.size = 1;
    L->strt.nuse = L->udt.nuse = 0;
    L->strt.hash[0] = L->udt.hash[0] = NULL;
}

void els_string_freeall(els_VmObj *L)
{
    L->nblocks -= (L->strt.size + L->udt.size) * sizeof(TString *);
    els_Mem_free(L, L->strt.hash);
    els_Mem_free(L, L->udt.hash);
}

static unsigned long hash_s(const char *s, size_t l)
{
    unsigned long h = l;     
    size_t step = (l >> 5) | 1; 
    for (; l >= step; l -= step)
        h = h ^ ((h << 5) + (h >> 2) + (unsigned char)*(s++));
    return h;
}

void els_string_resize(els_VmObj *L, stringunit *tb, int newsize)
{
    TString **newhash = els_Mem_newvector(L, newsize, TString *);
    int i;
    for (i = 0; i < newsize; i++)
        newhash[i] = NULL;
    
    for (i = 0; i < tb->size; i++)
    {
        TString *p = tb->hash[i];
        while (p)
        {                 
            TString *next = p->nexthash; 
            unsigned long h = (tb == &L->strt) ? p->u.s.hash : IntPoint(p->u.d.value);
            int h1 = h & (newsize - 1); 
            p->nexthash = newhash[h1]; 
            newhash[h1] = p;
            p = next;
        }
    }
    els_Mem_free(L, tb->hash);
    L->nblocks += (newsize - tb->size) * sizeof(TString *);
    tb->size = newsize;
    tb->hash = newhash;
}

static void newentry(els_VmObj *L, stringunit *tb, TString *ts, int h)
{
    ts->nexthash = tb->hash[h]; 
    tb->hash[h] = ts;
    tb->nuse++;
    if (tb->nuse > (lint32)tb->size && tb->size < MAX_INT / 2) 
        els_string_resize(L, tb, tb->size * 2);
}

TString *els_string_newlstr(els_VmObj *L, const char *str, size_t l)
{
    unsigned long h = hash_s(str, l);
    int h1 = h & (L->strt.size - 1);
    TString *ts;
    for (ts = L->strt.hash[h1]; ts; ts = ts->nexthash)
    {
        if (ts->len == l && (memcmp(str, ts->str, l) == 0))
            return ts;
    }
    ts = (TString *)els_Mem_malloc(L, sizestring(l));
    ts->marked = 0;
    ts->nexthash = NULL;
    ts->len = l;
    ts->u.s.hash = h;
    ts->u.s.constindex = 0;
    memcpy(ts->str, str, l);
    ts->str[l] = 0;
    L->nblocks += sizestring(l);
    newentry(L, &L->strt, ts, h1); 
    return ts;
}

TString *els_string_newudata(els_VmObj *L, size_t s, void *udata)
{
    union L_UTString *uts = (union L_UTString *)els_Mem_malloc(L,(lint32)sizeof(union L_UTString) + s);
    TString *ts = &uts->ts;
    ts->marked = 0;
    ts->nexthash = NULL;
    ts->len = s;
    ts->u.d.tag = 0;
    ts->u.d.value = (udata == NULL) ? uts + 1 : udata;
    L->nblocks += sizestring(s);
    newentry(L, &L->udt, ts, IntPoint(ts->u.d.value) & (L->udt.size - 1));
    return ts;
}

TString *els_string_createudata(els_VmObj *L, void *udata, int tag)
{
    int h1 = IntPoint(udata) & (L->udt.size - 1);
    TString *ts;
    for (ts = L->udt.hash[h1]; ts; ts = ts->nexthash)
    {
        if (udata == ts->u.d.value && (tag == ts->u.d.tag || tag == ELS_ANYTAG))
            return ts;
    }
    ts = els_string_newudata(L, 0, udata);
    if (tag != ELS_ANYTAG)
        ts->u.d.tag = tag;
    return ts;
}

TString *els_string_new(els_VmObj *L, const char *str)
{
    return els_string_newlstr(L, str, strlen(str));
}

TString *els_string_newfixed(els_VmObj *L, const char *str)
{
    TString *ts = els_string_new(L, str);
    if (ts->marked == 0)
        ts->marked = FIXMARK; 
    return ts;
}

